//===============================================================================
// Microsoft patterns & practices Enterprise Library Contribution
// Resource Application Block
//===============================================================================

using System;
using System.Configuration;
using System.Diagnostics.CodeAnalysis;
using Microsoft.Practices.EnterpriseLibrary.Common.Configuration;
using EntLibContrib.Resource.Configuration;
using EntLibContrib.Resource.Properties;

namespace EntLibContrib.Resource
{
	/// <summary>
	/// Represents a view to navigate the <see cref="ResourceSettings"/> configuration data.
	/// </summary>
	public class ResourceConfigurationView
	{
		#region Fields
		private IConfigurationSource configurationSource;
		#endregion

		#region Properties
		/// <summary>
		/// Gets the name of the default <see cref="ResourceManagerData"/>.
		/// </summary>
		/// <returns>
		/// The name of the default <see cref="ResourceManagerData"/>.
		/// </returns>
		[SuppressMessage("Microsoft.Design", "CA1065:DoNotRaiseExceptionsInUnexpectedLocations")]
		public string DefaultResourceManager
		{
			get
			{
				ResourceSettings configSettings = this.ResourceSettings;
				if (string.IsNullOrEmpty(configSettings.DefaultResourceManager))
					throw new ConfigurationErrorsException(InternalResources.NoDefaultResourceManager);

				return configSettings.DefaultResourceManager;
			}
		}

		/// <summary>
		/// Gets the <see cref="ResourceSettings"/> configuration data.
		/// </summary>
		/// <returns>
		/// The <see cref="ResourceSettings"/> configuration data.
		/// </returns>
		[SuppressMessage("Microsoft.Design", "CA1065:DoNotRaiseExceptionsInUnexpectedLocations")]
		public ResourceSettings ResourceSettings
		{
			get 
			{
				ResourceSettings settings = (ResourceSettings)configurationSource.GetSection(ResourceSettings.SectionName);
				if (settings == null)
					throw new ConfigurationErrorsException(InternalResources.MissingResourceSection);

				return settings;
			}
		}
		#endregion

		#region Construction
		/// <summary>
		/// Initialize a new instance of the <see cref="ResourceConfigurationView"/> with an <see cref="IConfigurationSource"/>.
		/// </summary>
		/// <param name="configurationSource">An <see cref="IConfigurationSource"/> object.</param>
		public ResourceConfigurationView (IConfigurationSource configurationSource)
		{
			if (configurationSource == null)
				throw new ArgumentNullException("configurationSource");

			this.configurationSource = configurationSource;
		}
		#endregion

		#region Public Methods
		/// <summary>
		/// Gets the <see cref="ResourceManagerDataBase"/> from configuration for the named <see cref="IResourceManager"/>
		/// </summary>
		/// <param name="resourceManagerName">The name of the <see cref="IResourceManager"/>.</param>
		/// <returns>
		/// A <see cref="ResourceManagerDataBase"/> object.
		/// </returns>
		public ResourceManagerDataBase GetResourceManagerData (string resourceManagerName)
		{
			if (string.IsNullOrEmpty(resourceManagerName))
				throw new ConfigurationErrorsException(string.Format(InternalResources.Culture, InternalResources.ExceptionResourceManagerNameNullOrEmpty, resourceManagerName));

			ResourceSettings configSettings = this.ResourceSettings;
			if (!configSettings.ResourceManagers.Contains(resourceManagerName))
				throw new ConfigurationErrorsException(string.Format(InternalResources.Culture, InternalResources.UnableToFindResourceManagerInstance, resourceManagerName));

			return configSettings.ResourceManagers.Get(resourceManagerName);
		}

		/// <summary>
		/// Gets the <see cref="ResourceProviderDataBase"/> from configuration for the named <see cref="IResourceProvider"/>
		/// </summary>
		/// <param name="resourceProviderName">The name of the <see cref="IResourceProvider"/>.</param>
		/// <returns>
		/// A <see cref="ResourceProviderDataBase"/> object.
		/// </returns>
		public ResourceProviderDataBase GetResourceProviderData (string resourceProviderName)
		{
			if ( string.IsNullOrEmpty(resourceProviderName) )
				throw new ConfigurationErrorsException(string.Format(InternalResources.Culture, InternalResources.ExceptionResourceProviderNameNullOrEmpty, resourceProviderName));

			ResourceSettings configSettings = this.ResourceSettings;
			if (!configSettings.ResourceProviders.Contains(resourceProviderName))
				throw new ConfigurationErrorsException(string.Format(InternalResources.Culture, InternalResources.ExceptionProviderNotDefined, resourceProviderName));

			return configSettings.ResourceProviders.Get(resourceProviderName);
		}
		#endregion
	}
}